package com.example.zhaoping.controller;

import com.example.zhaoping.entity.Message;
import com.example.zhaoping.entity.MessageList;
import com.example.zhaoping.serviceImpl.MessageServiceImpl;
import com.example.zhaoping.util.Result;
import com.example.zhaoping.util.TokenUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MessageController {
    @Resource
    private MessageServiceImpl messageService;
    @Resource
    private TokenUtil tokenUtil;
    @Resource
    private Result result;

    @GetMapping("/enterprise/getMsgList")
    public Result<?> getMsgCount(@RequestHeader("Authorization") String token){
        int id = tokenUtil.getUserId1(token);
        if(id!=0){
            List messageLists = messageService.selectMsgList("enterprise",id);
            return result.success(messageLists);
        }else{
            return result.error("token解析失败！");
        }
    }
    //把聊天记录set为已读
    @GetMapping("/setReaded")
    public Result<?> setReaded(@RequestHeader("Authorization") String token,
                               @PathParam("formId") int formId,@PathParam("formType") String formType,
                               @PathParam("toId") int toId,@PathParam("toType") String toType
                               ){
        int id = tokenUtil.getUserId1(token);
        System.out.println(formId);
        System.out.println(formType);
        System.out.println(toId);
        System.out.println(toType);
        if(id!=0){
            int readed = messageService.setReaded(formId,formType,toId,toType);
            return result.success("");
        }else{
            return result.error("身份验证失败，请重新登录！");
        }
    }

    @GetMapping("/user/getMsgList")
    public Result<?> getMsgCount1(@RequestHeader("Authorization") String token){
        int id = tokenUtil.getUserId1(token);
        if(id!=0){
            List messageLists = messageService.selectMsgList("user",id);
            return result.success(messageLists);
        }else{
            return result.error("token解析失败！");
        }
    }
}
